#ifndef _STRFIX_H_
#define _STRFIX_H_

#include <stdint.h>
#include <stdlib.h>
#include <string.h>


#define scatf(str, fmt, arg...) \
    do { \
        sprintf((str) + strlen(str), fmt, ## arg); \
    } while(0)


static inline int strint(char *src, int *out)
{
    for (int i = 0; src[i] != '\0'; i++) {
        if (src[i] < '0' || src[i] > '9') {
            printf("src: %s\n", src);
            printf("strint: [%c], i=%d\n", src[i], i);
            return -1;
        }
    }
    *out = atoi(src);
    return 0;
}

static inline int strint64(char *src, int64_t *out)
{
    for (int i = 0; src[i] != '\0'; i++) {
        if (src[i] < '0' || src[i] > '9') {
            return -1;
        }
    }

    *out = atoll(src);

    return 0;
}

static inline char *strbtwn(char *src, char *start, char *end, char *out)
{
    char *s = strstr(src, start);
    if (s == NULL) {
        return NULL;
    }

    s += strlen(start);

    char *p = strstr(s, end);
    if (p == NULL) {
        return NULL;
    }

    strncpy(out, s, p - s);
    out[p - s] = '\0';
    p += strlen(end);

    return p;
}

#endif
